package com.hzyl.famousreader.view.widget.core.animation;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Scroller;

/**
 * 翻页动画基类
 */
public abstract class AnimAbs {

  /**
   * 正在使用该动画的View
   */
  protected View mView;

  /**
   * 屏幕尺寸
   */
  protected int mScreenWidth;
  protected int mScreenHeight;

  /**
   * 屏幕间距
   */
  protected int mMarginWidth;
  protected int mMarginHeight;

  /**
   * 视图尺寸
   */
  protected int mViewWidth;
  protected int mViewHeight;

  /**
   * 滑动装置
   */
  protected Scroller mScroller;

  /**
   * 翻页方向
   */
  protected AnimDirectionEnum mDirection = AnimDirectionEnum.NONE;

  /**
   * 运动状态
   */
  protected boolean isRunning = false;

  /**
   * 触摸的起始点
   */
  protected float mStartX;
  protected float mStartY;

  /**
   * 当前的触摸点
   */
  protected float mTouchX;
  protected float mTouchY;

  /**
   * 上一个触摸点
   */
  protected float mLastX;
  protected float mLastY;

  /**
   * 翻页事件监听器
   */
  protected OnPageChangeListener mListener;

  public AnimAbs(View mView, int w, int h, OnPageChangeListener listener) {
    this.mView = mView;
    this.mListener = listener;

    mScreenWidth = w;
    mScreenHeight = h;

    mViewWidth = w;
    mViewHeight = h;

    mScroller = new Scroller(mView.getContext());
  }

  /**
   * 设置最初的触摸点
   */
  public void setStartPoint(float x, float y) {
    mStartX = x;
    mStartY = y;

    mLastX = mStartX;
    mLastY = mStartY;
  }

  /**
   * 设置当前触摸点
   */
  public void setTouchPoint(float x, float y) {
    mLastX = mTouchX;
    mLastY = mTouchY;

    mTouchX = x;
    mTouchY = y;
  }

  /**
   * 绘制
   */
  public abstract void draw(Canvas canvas);

  /**
   * 点击事件处理
   */
  public abstract boolean onTouchEvent(MotionEvent event);

  /**
   * 获取即将要展示的bitmap(进行背景绘制)
   */
  public abstract Bitmap getBgBitmap();

  /**
   * 获取即将要展示的bitmap(进行内容绘制)
   */
  public abstract Bitmap getShowBitmap();

  /**
   * 让动画进行滚动，注意：必须放在computeScroll()中执行
   */
  public abstract void scrollAnim();

  /**
   * 开启翻页动画
   */
  protected abstract void startAnim();

  /**
   * 取消翻页动画
   */
  protected abstract void abortAnim();

  /**
   * 设置翻页方向
   *
   * @param direction
   */
  protected void setDirection(AnimDirectionEnum direction) {
    mDirection = direction;
  }

  /**
   * 翻页事件回调接口
   */
  public interface OnPageChangeListener {

    /**
     * 往前翻回调事件
     *
     * @return true-继续翻页 false-已经是第一页了
     */
    boolean prev();

    /**
     * 往后翻回调事件
     *
     * @return true-继续翻页 false-已经是最后一页了
     */
    boolean next();

    /**
     * 取消翻页回调事件
     */
    void cancel();
  }
}
